package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private JedisPool jedisPool;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        //保存完成套餐信息之后把图片名称存储到redis集合中
        String fileName = setmeal.getImg();
        this.method(setmealId,checkgroupIds);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);

        //生成静态页面
        this.generateMobileStaticHtml();
    }
    @Autowired
    private FreeMarkerConfigurer configurer;
    @Value("${out_put_path}")
    private String outPutPath;
    //提供统一的静态页面方法，用来生成套餐详情页和套餐列表详情页
    public void generateMobileStaticHtml(){
        //查询所有的setmeal信息
        List<Setmeal> list = setmealDao.findAll();
       /* for (Setmeal setmeal : list) {
            System.out.println(setmeal.getRemark());
        }*/
        //生成套餐列表静态页面
        this.generateMobileSetmealListHtml(list);
        //给套餐列表中的所有套餐生成套餐详情页（检查组和检查项）
        this.generateMobileSetmealDetailHtml(list);

    }
    //生成套餐列表页面
    public void generateMobileSetmealListHtml(List<Setmeal> list){
        Map map=new HashMap();
        map.put("setmealList",list);
        this.generteHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }
    //生成单个套餐的详情页
    public void generateMobileSetmealDetailHtml(List<Setmeal> list){
        for (Setmeal setmeal : list) {
            Map map=new HashMap();
            //根据id 查找套餐对应的检查组和检查项
            map.put("setmeal",setmealDao.findById(setmeal.getId()));
            this.generteHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }
    }

    //提供方法用于前期准备和后期写出
    public void generteHtml(String templateName,String htmlPageName,Map map){
        Configuration configuration=configurer.getConfiguration();
        try {
            Template template = configuration.getTemplate(templateName);
            Writer writer=new FileWriter(new File(outPutPath+"/"+htmlPageName));
            template.process(map,writer);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override//给手机端返回套餐数据
    public List<Setmeal> findAll() {
        List<Setmeal> list = setmealDao.findAll();
        return list;
    }

    //根据mobile的id查询相对应的检查组和检查项
    public Setmeal findById(Integer id){
        return setmealDao.findById2(id);
    }

    @Override//获取套餐统计报表数据
    public List<Map<String, Object>> findSetmealCount() {
        List setmealCount=setmealDao.findSetmealCount();
        return setmealCount;
    }

    public void method(Integer setmealId,Integer[] checkgroupIds ){
        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map=new HashMap<>();
            map.put("setmealId",setmealId);
            map.put("checkgroupId",checkgroupId);
            setmealDao.addSetAndGroup(map);
        }
    }
}
